package com.hospital.common.core.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * @author lvyao
 * @description 汉字转拼音
 * @date 2020/7/1
 */
public class PinYinKit {
    public static String getHanziInitials(String hanzi) {
        String result = null;
        if (null != hanzi && !"".equals(hanzi)) {
            char[] charArray = hanzi.toCharArray();
            StringBuffer buffer = new StringBuffer();
            for (char ch : charArray) {
                String[] stringArray = PinyinHelper.toHanyuPinyinStringArray(ch);
                if (null != stringArray) {
                    buffer.append(stringArray[0].replaceAll("\\d", "") + " ");
                }
            }
            if (buffer.length() > 0) {
                result = buffer.toString();
            }
        }
        return result;
    }
}
